package link.tengfei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import link.tengfei.bean.AdminUser;
import link.tengfei.util.ConnectionFactory;

public class AdminLoginDaoImpl implements AdminLoginDao {

	@Override
	public AdminUser findUserByUsrAndPwd(String username, String password) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		String sql = "select id,name,username ,judge from admin_user where username=? and password=?";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, username);
			psmt.setString(2, password);
			rs = psmt.executeQuery();
			while (rs.next()) {
				AdminUser adminUser = new AdminUser();
				adminUser.setId(rs.getInt("id"));
				adminUser.setName(rs.getString("name"));
				adminUser.setUsername(rs.getString("username"));
				adminUser.setJudge(rs.getInt("judge"));
				return adminUser;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return null;
	}

}
